{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12ce3267",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "age:10 money:200 name:Test\n",
      "0\n",
      "100\n",
      "150\n",
      "Hello World\n"
     ]
    }
   ],
   "source": [
    "/**\n",
    "1. var隐式类型,它可以表示任意类型的变量\n",
    "    - var不能作为类的成员变量,只能用于临时变量申明\n",
    "    - var必须初始化\n",
    "*/\n",
    "\n",
    "var i = 10;\n",
    "var array =new int[] {1,3,4};\n",
    "\n",
    "/**\n",
    "2. 设置对象初始值\n",
    "*/\n",
    "\n",
    "class Person {\n",
    "    private int age;\n",
    "    public bool sex;\n",
    "    public string Name{get;set;}\n",
    "}\n",
    "\n",
    "Person person = new Person { sex=true, Name = \"Test\"};\n",
    "\n",
    "/**\n",
    "3. 声明集合的初始值\n",
    "*/\n",
    "array = new int[] {1,2,3,4,5};\n",
    "List<int> list = new List<int>{1,2,3,4,5};\n",
    "List<Person>persons= new List<Person> {\n",
    "    new Person(),\n",
    "    new Person() {sex = true},\n",
    "    new Person{sex = true, Name = \"Test\"}\n",
    "};\n",
    "\n",
    "/**\n",
    "4. 匿名类型,中间不能写函数,只能有成员变量\n",
    "*/\n",
    "var v = new { age = 10, money = 200, name = \"Test\"};\n",
    "Console.WriteLine(\"age:\"+v.age+\" money:\"+v.money+\" name:\"+v.name);\n",
    "\n",
    "/**\n",
    "5. 可空类型?,使用时需要判断是否为空\n",
    "*/\n",
    "int? c = null;\n",
    "if(c.HasValue) {\n",
    "    Console.WriteLine(c);\n",
    "}\n",
    "Console.WriteLine(c.GetValueOrDefault()); //如果为空则打印默认值,并没有给c赋值\n",
    "Console.WriteLine(c.GetValueOrDefault(100)); //如果为空则打印100,并没有给c赋值\n",
    "object o = null;\n",
    "o?.ToString(); //当o为空不会调用\n",
    "\n",
    "/**\n",
    "6. 空合并操作符 a??b 如果a为空则返回b,否则返回a (只要是a可以为空都可以用)\n",
    "*/\n",
    "Console.WriteLine(c ?? 150);\n",
    "\n",
    "/**\n",
    "7. 内插字符串:$ 使用$构造字符串,让字符串中可以拼接变量\n",
    "*/\n",
    "string str = \"Hello\";\n",
    "Console.WriteLine($\"{str} World\");"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "polyglot-notebook"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "name": "csharp"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
